<template>
	<view>
		<view class=" ">
			<view class="input d-flex">
				<image src="@/static/img/login/yanzhengma.png" mode="widthFix" style="width: 40rpx;"></image>
				<input class="flex-1" type="password" v-model="formData.password" name="password" placeholder="请输入新密码"
					password :maxlength="11">
			</view>
			<view class="d-flex input">
				<image src="@/static/img/login/yanzhengma.png" mode="widthFix" style="width: 40rpx;"></image>
				<input class="flex-1" type="password" v-model="formData.repassword" name="repassword"
					placeholder="请再次输入新密码" password :maxlength="11">
			</view>
		</view>
		<button class="btn" @click="change">提交</button>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				title: '新登录密码',
				code:'',
				formData: {
					password: '',
					repassword: ''
				},
			};
		},
		onLoad(options) {
			let result = this.$api.isLogin();
			if (result === false) {
				return false;
			}
			
			this.code = options.code;

		},
		methods: {
			...mapMutations(['login', 'logout']),
			
			change() {
				if (!this.code) {
					this.$api.msg('非法操作！');
					this.logout();
					uni.setStorageSync('token', '')
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}, 600)
				}
				if (this.formData.password.length < 6) {
					this.$api.msg('登录密码最少6位数！');
					return false;
				}
				for (let i = 0; i < this.formData.password.length; i++) {
					let newWord = this.formData.password.substr(i, 1);
					if (!/[0-9a-zA-Z]/.test(newWord)) {
						this.$api.msg('密码只能输入大小写字母和数字');
						return false;
					}
				}
				if (this.formData.password !== this.formData.repassword) {
					this.$api.msg('登录密码和确认密码不一致！');
					return false;
				}

				let dataObj = {
					token: uni.getStorageSync('token'),
					code: this.code,
					password: this.formData.password
				};
				this.submit(dataObj);
			},

			async submit(dataObj) {
				let res = await this.$myRequest({
					url: '/changePwd1',
					data: dataObj,
					methods: 'post'
				})

				if (res.data.status == 1) {
					this.$api.msg(res.data.msg);
					uni.setStorageSync('token', '')
					this.logout();
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}, 1500)
				}
			}
		},
	}
</script>

<style lang="scss">
	page {
		background-color: #f5f5f5;
	}

	.input {
		width: 82%;
		height: 100%;
		margin: 20rpx auto;
		border: 2rpx solid #f5f5f5;
		background-color: white;
		padding: 30rpx;
		border-radius: 20rpx;
	}

	input {
		padding: 0 20rpx;
	}

	.btn {
		border-radius: 10rpx;
		color: white;
		background-color: #e80909;
		margin: 40rpx;
	}
</style>
